package com.aaa.sbm.service.impl;

import com.aaa.sbm.entity.User;
import com.aaa.sbm.dao.UserDao;
import com.aaa.sbm.service.UserService;
import com.aaa.sbm.util.ConstantUtil;
import org.apache.shiro.crypto.hash.Sha512Hash;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.aaa.sbm.util.CustomException;
import com.aaa.sbm.util.ResultStatus;
import com.aaa.sbm.util.Page;
import org.springframework.util.CollectionUtils;


import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 用户信息表(User)表服务实现类
 *
 * @author makejava
 * @since 2024-04-09 11:12:26
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;

    @Resource
    private HttpSession session;

    @Override
    public User queryByUserName(String userName) {
        //拼装查询条件
        User user = new User();
        user.setUserName(userName);
        user.setStatus("0");
        user.setDelFlag("0");
        //查询
        List<User> userList = userDao.queryAllByLimit(user);
        //判断
        if(!CollectionUtils.isEmpty(userList)){
            //返回查询到第一个元素
            return userList.get(0);
        }
        return null;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param userId 主键
     * @return 实例对象
     */
    @Override
    public User queryById(Integer userId) {
        return this.userDao.queryById(userId);
    }

    /**
     * 分页查询
     *
     * @param page 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo queryByPage(Page<User> page) {
        //分页操作  分页参数一定要进行传递
        if (page == null || page.getPageNo() == null || page.getPageNo() == 0 || page.getPageSize() == null || page.getPageSize() == 0) {
            //进行业务编写时，可以抛出自定义异常
            throw new CustomException(ResultStatus.PARAM_NOT_EMPTY.getReturnCode(),
                    ResultStatus.PARAM_NOT_EMPTY.getReturnMessage());
        }
        PageHelper.startPage(page.getPageNo(), page.getPageSize());
        //获取查询对象
        User user = page.getData();
        return new PageInfo(userDao.queryAllByLimit(user));
    }

    /**
     * 新增数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User insert(User user) {
        //随机盐值
        String salt  = UUID.randomUUID().toString();
        //获取用户输入的密码 password = 123456
        String oldPassword = user.getPassword();
        //计算新密码
        Sha512Hash sha512Hash = new Sha512Hash(oldPassword,salt,
                ConstantUtil.ShiroCredentialsMatcher.HASH_ITERATIONS);
        user.setSalt(salt);
        user.setPassword(sha512Hash.toString());
        //获取session
        User sessionUser= (User)session.getAttribute("userInfo");
        if(sessionUser!=null){
            String realName = sessionUser.getRealName();
            user.setCreateBy(realName);
            user.setUpdateBy(realName);
        }

        Date date = new Date();
        user.setCreateTime(date);
        user.setUpdateTime(date);
        user.setStatus(ConstantUtil.userStatus.ENABLE);
        user.setDelFlag(ConstantUtil.userDeleteFlag.DELETE);
        this.userDao.insert(user);
        return user;
    }

    /**
     * 修改数据
     *
     * @param user 实例对象
     * @return 实例对象
     */
    @Override
    public User update(User user) {
        this.userDao.update(user);
        return this.queryById(user.getUserId());
    }

    /**
     * 通过主键删除数据
     *
     * @param userId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer userId) {
        return this.userDao.deleteById(userId) > 0;
    }
}
